<template>
  <main-page>
    <div slot="body">
      <el-form :inline="true" :model="search" size="mini">
        <el-form-item>
          <el-button type="primary" @click="dialogFormVisible = true;">新增</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" style="width: 100%" tooltip-effect="dark">
        <el-table-column prop="id" label="编号"></el-table-column>
        <el-table-column label="父节点">
          <template slot-scope="scope">
            <el-tag type="info" v-if="scope.row.parentId == 0">父节点</el-tag>
            <el-tag v-else>
              {{scope.row.baseDataVo != undefined ? scope.row.baseDataVo.dataName : '暂无父节点'}}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="dataName" label="字典名称"></el-table-column>
        <el-table-column prop="dataKey" label="字典key"></el-table-column>
        <el-table-column prop="dataDesc" label="字典描述"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              title="编辑"
              size="small"
              @click="handleClick2EditById(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              title="删除"
              size="small"
              @click="handleClick2DeleteById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination :listNum="listNum" @currentChange="handleCurrentChange"></pagination>

      <el-dialog title="字典管理" :visible.sync="dialogFormVisible" width="40%">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="父节点" prop="parentId">
            <el-select v-model="form.parentId" style="width:100%">
              <el-option :key="0" label="父节点" :value="0"></el-option>
              <el-option v-for="item in parentList" :key="item.id" :label="item.dataName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="字典名称" prop="dataName">
            <el-input v-model="form.dataName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="字典key" prop="dataKey">
            <el-input v-model="form.dataKey" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="字典描述" prop="dataDesc">
            <el-input v-model="form.dataDesc" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="onSubmit">提交</el-button>
        </div>
      </el-dialog>
    </div>
  </main-page>
</template>

<script>
import {
  getBaseDataList,
  saveBaseData,
  deleteBaseData,
  updateBaseDataById,
  getBaseDataAllList
} from "api/BaseDataApi";
export default {
  name: "BaseDataList",
  data () {
    return {
      tableData: [],
      listNum: 1,
      search: {
        currentIndex: 1
      },
      dialogFormVisible: false,
      form: {
        id: '',
        parentId: 0,
        dataKey: '',
        dataDesc: '',
        dataName: ''
      },
      rules: {
        parentId: [
          {required: true, message: '请选择父节点'}
        ],
        dataName: [
          {required: true, message: '请输入字典名称'}
        ],
        dataKey: [
          {required: true, message: '请输入字典key'}
        ]
      },
      parentList: []
    }
  },
  methods: {
    getBaseDataList () {
      getBaseDataList (this.search)
        .then(res => {
          this.tableData = res.datas;
          this.listNum = res.pagination.totalCount;
        })
    },
    handleCurrentChange(page) {
      this.search.currentIndex = page;
      this.getBaseDataList();
    },
    onSubmit () {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.id) {
            //修改
            updateBaseDataById(this.form).then(res => {
              if (res) {
                this.$base.showOk();
                this.dialogFormVisible = false;
                this.getBaseDataList();
              }
            });
          } else {
            //保存
            saveBaseData(this.form).then(res => {
              if (res) {
                this.$base.showOk();
                this.dialogFormVisible = false;
                this.getBaseDataList();
              }
            });
          }
        }
      });
    },
    handleClick2EditById (row) {
      this.dialogFormVisible = true;
      this.$base.set2Form(this.form, row);
      this.form.parentId = Number(row.parentId)
    },
    handleClick2DeleteById (id) {
      this.$base.deleteConfirm(() => {
        deleteBaseData({ id }).then(res => {
          if (res) {
            this.$base.deleteOk();
            this.getBaseDataList();
          }
        });
      });
    },
    getBaseDataAllList () {
      getBaseDataAllList ().then(res => {
        if(res) {
          this.parentList = res
        }
      })
    }
  },
  created () {
    this.getBaseDataList()
  },
  watch: {
    dialogFormVisible(newDialogFormVisible) {
      if (!newDialogFormVisible) {
        this.$base.clearObj(this.form);
        this.$refs.form.resetFields();
      } else  {
        this.getBaseDataAllList();
      }
        
    }
  }
};
</script>

<style lang="stylus" scoped></style>
